package game.game2;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class mygame2 extends Frame {
    public static final int GAME_WIDTH = 1000;
    public static final int GAME_HEIGHT = 700;
    Image fj = gameutil.getImage("img/fj.jpg");
    Image bj = gameutil.getImage("img/bj.jpg");
    static int count = 0;

    gameobject fjtp = new gameobject(fj,100,100,3,22,33);




    @Override
    public void paint(Graphics g) {   //g当做是一支画笔
        System.out.println("绘制窗口次数"+count);
        count++;
       g.drawImage(bj, 0,0,1000,700,null);
       fjtp.drawmyself(g);




    }

    public void launchFrame(){
        this.setTitle("飞机大战！！！");
        setVisible(true);   //窗口是否可见

        setSize(GAME_WIDTH, GAME_HEIGHT);  //窗口大小
        setLocation(400,300);
        //关闭窗口动
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        new paintThread().start();  // 启动重画窗口线程
/*        this.addKeyListener(new KeyMonitor());   //启动键盘监听*/

    }
    /**
     * 重画窗口线程
     * */
    class paintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();   //内部类可以使用外部类成员

                try {
                    Thread.sleep(10);       //1s==1000ms   画20次一秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //java内部类，实现键盘监听处理
    class keymonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("按下："+e.getKeyCode());
        }
        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("抬起："+e.getKeyCode());

        }
    }



    private Image offscreenImage = null;

    public void update(Graphics g) {
        if (offscreenImage == null)
            offscreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);    //游戏窗口的宽和高

        Graphics goff = offscreenImage.getGraphics();
        paint(goff);
        g.drawImage(offscreenImage, 0, 0, null);
    }


    public static void main(String[] args) {
        mygame2 game = new mygame2();
        game.launchFrame();
    }
}
